package bgu.eliav;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;

public class ItemsList extends ListActivity {
	List<HashMap<String, String>> strList;
	static final int DIALOG_EXIT = 0;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	protected void onListItemClick(ListView parent, View v, int position,
			long id) {
		Log.d("class3", "onListItemClick " + position);
		User user = new User(strList.get(position).get("name"), strList.get(
				position).get("id"), strList.get(position).get("phone"),
				strList.get(position).get("address"), strList.get(position)
						.get("bdate"));
		Intent intent = new Intent(this, ViewItem.class);
		intent.putExtra("bgu.eliav.extra", user);
		startActivity(intent);
	}

	protected void onResume() {
		super.onResume();
		SharedPreferences ref = getSharedPreferences("bgu.eliav", MODE_PRIVATE);
		String check = ref.getString("action", "");
		if (check.equals("exit")) {
			SharedPreferences.Editor ed = ref.edit();
			ed.putString("action", "exit");
			ed.commit();
			finish();
		}

		setContentView(R.layout.items_list);
		DatabaseExample db = DatabaseExample.getInstance(this);
		strList= db.getContacts(this);;
		SimpleAdapter adapter = new SimpleAdapter(this,
				strList,
				R.layout.list_row,
				new String[]{"id","name"},
				new int[]{R.id.ListTextView01, R.id.ListTextView02});  

		setListAdapter(adapter);
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu1, menu);
		return true;

	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.exit:
			showDialog(DIALOG_EXIT);

		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog = null;
		switch (id) {
		case DIALOG_EXIT:
			return creatingExitDialog();

		}

		return dialog;
	}

	private Dialog creatingExitDialog() {
		final Activity localActivity = this;
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Are you sure you want to exit?");
		builder.setCancelable(false);

		builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				SharedPreferences ref = getSharedPreferences("bgu.eliav",
						MODE_PRIVATE);
				SharedPreferences.Editor ed = ref.edit();
				ed.putString("action", "exit");
				ed.commit();
				localActivity.finish();
			}
		});

		builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				dialog.cancel();
			}
		});
		AlertDialog alert = builder.create();
		return alert;

	}

}
